package com.yingsheng.bi.hks.template.processor.impl;

import com.yingsheng.bi.hks.template.events.Event;
import com.yingsheng.bi.hks.template.events.MethodEvent;
import com.yingsheng.bi.hks.template.processor.InnerProcessor;
import com.yingsheng.bi.hks.template.utils.JacksonUtils;
import org.springframework.beans.BeanUtils;

import java.util.Map;

public class Request2MapProcessor extends InnerProcessor {

    @Override
    public void execInner(Event event) {
        if (event instanceof MethodEvent) {
            MethodEvent methodEvent = (MethodEvent) event;
            Object request = methodEvent.getRequest();
            if (request != null) {
                if (BeanUtils.isSimpleProperty(request.getClass())) {
                    super.execInner(event);
                } else if (request instanceof Map) {
                    methodEvent.putAll((Map<String, Object>) request);
                } else {
                    methodEvent.putAll(JacksonUtils.obj2Map(request));
                }
            } else {
                super.execInner(event);
            }
        } else {
            super.execInner(event);
        }
    }
}
